home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3830 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: array passing problem
  5. Date: Wed, 31 Jan 1996 13:21:30 GMT
  6. Organization: Netcom
  7. Message-ID: <310f6bb8.49956992@nntp.ix.netcom.com>
  8. References: <cl3kj4C00bkWMWlEJH@andrew.cmu.edu>
  9. NNTP-Posting-Host: ix-dc6-05.ix.netcom.com
  10. X-NETCOM-Date: Wed Jan 31  5:21:51 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Chie B Ng <cn0v+@andrew.cmu.edu> wrote:
  14.  
  15. > Hello, sorry to bother you, but I can not see what the problem is with
  16. > this code fragment.  When I use gcc -ansi to compile this I receive this
  17. > message: "warning: passing arg 1 of 'average' from incompatible pointer
  18. > type".  When I run it, I get a segmentation fault in the average
  19. > procedure.  Thanks.
  20. > /* headers go here.... */
  21. > typedef unsigned char pixel;
  22. > double average(pixel **a, int rows, int columns)
  23. > {
  24. >     int i, j, sum = 0;
  25. >     /* since char is an int, there is no problem with + */
  26. >     for (i = 0 ; i < rows ; i++)
  27. >         for (j = 0 ; j < columns ; j++)
  28. >             sum += a[i][j];
  29. >     return ((double)sum)/(rows*columns);
  30. > }
  31. >     
  32. > void main(int argc, char **argv[])
  33. > {
  34. >     double avg;
  35. >     pixel image[200][100];
  36. >     /* initialize image with values */
  37. >     /* .... */
  38. >     avg = average(image, 200, 100);
  39. >     printf("%f", avg);
  40. >     exit(0);
  41. > }
  42.  
  43. In average, the first parameter is a pointer to pointer to pixel.  The
  44. argument you are passing is an array of array of pixel.  These are
  45. different and incompatible types.
  46.  
  47. In most expressions an array is converted to a pointer, but that's
  48. all.  In the call to average image is converted to a pointer to an
  49. array of 100 pixel (pixel (*)[100]).  It is not converted to a pointer
  50. to pointer to pixel.
  51.  
  52. Either change the definition of average to accept an array, e.g.,
  53.  
  54.     double average(pixel a[][100], int rows, int columns)
  55.  
  56. or change main to define image as
  57.  
  58.     pixel **image;
  59.  
  60. In the latter case you'll have to allocate memory for image.
  61.  
  62.  
  63. Michael M Rubenstein
  64.